<template>
	<view class="setting-container">
		<common-head title="新增人员" :autoBack="true"></common-head>
		<view class="farm-content">
			<view class="form-item required">
				<rh-input 
				label="姓名" 
				placeholder="请输入姓名" 
				v-model="params.cbfmc" 
				inputAlign="right">
				</rh-input>
			</view>
			<view class="form-item required">
				<rh-input 
				label="身份证号码" 
				placeholder="请输入身份证号码" 
				v-model="params.cbfsfzhm" 
				inputAlign="right">
				</rh-input>
			</view>
			<view class="form-item required">
				<rh-picker 
				label="人员情况" 
				v-model="params.ryqk" 
				:columns="getDictItem('elyb_ryqk')" 
				picker-title="选择人员情况" 
				placeholder="请选择人员情况">
				</rh-picker>
			</view>
			<view class="form-item required">
				<rh-picker 
				label="户主关系" 
				v-model="params.yhzgx" 
				:columns="getDictItem('elyb_yhzgx')" 
				picker-title="选择户主关系" 
				placeholder="请选择户主关系">
				</rh-picker>
			</view>
			<view class="form-item required">
				<rh-picker 
				label="职业" 
				v-model="params.zy" 
				:columns="getDictItem('elyb_zy')" 
				picker-title="选择职业" 
				placeholder="请选择职业">
				</rh-picker>
			</view>
			<view class="form-item">
				<rh-input 
				label="联系方式" 
				placeholder="请输入联系方式" 
				v-model="params.lxdh" 
				inputAlign="right">
				</rh-input>
			</view>
			<view class="form-item">
				<rh-radio 
				label="是否参加二轮承包" 
				v-model="params.sfcjelcb" 
				:radioList="getDictItem('elyb_yesno')" 
				inputAlign="right">
				</rh-radio>
			</view>
			<view class="form-item">
				<rh-datetime-picker 
				label="备注年月"
				placeholder="选择年月" 
				mode="year-month"
				v-model="params.bznf">
				</rh-datetime-picker>
			</view>
		</view>
		<rh-submit-btn ref="submit-btn" text="立即提交" @submit="submit"></rh-submit-btn>
	</view>
</template>

<script>
	import {
		validateIDCard, validatePhone
	} from '@/script/tools.js'
	import { insertCbf } from '@/script/api/rkxx/index.js'
	export default {
		data() {
			return {
				params: {},
				dictsList: [
					// 数据字典s
					{
						label: "人员情况",
						name: "elyb_ryqk",
						child: []
					},
					{
						label: "与户主关系",
						name: "elyb_yhzgx",
						child: []
					},
					{
						label: "职业",
						name: "elyb_zy",
						child: []
					},
					{
						label: "是否",
						name: "elyb_yesno",
						child: []
					},
				]
			}
		},
		created(){
			let _self = this
			this.initGetDicts()
		},
		methods: {
			getDictItem(name){
				let _self = this
				let dict = _self.dictsList.find(i => i.name == name)
				return dict.child
			},
			submit() {
				let _self = this, el = this.$refs['submit-btn']
				const { cbfmc, cbfsfzhm, ryqk, yhzgx, zy, lxdh } = _self.params
				let sfzhItem = validateIDCard(cbfsfzhm)
				if(!cbfmc){
					_self.$attention.noIcon('姓名不能为空')
					return el['submit_loading'] = false
				}
				if(!sfzhItem){
					_self.$attention.noIcon('请输入正确格式的身份证号')
					return el['submit_loading'] = false
				}else{
					_self.$set(_self.params, 'cbfxb', sfzhItem.gender)
				}
				if(!ryqk){
					_self.$attention.noIcon('人员情况不能为空')
					return el['submit_loading'] = false
				}
				if(!yhzgx){
					_self.$attention.noIcon('与户主关系不能为空')
					return el['submit_loading'] = false
				}
				if(!zy){
					_self.$attention.noIcon('职业不能为空')
					return el['submit_loading'] = false
				}
				if(lxdh && validatePhone(lxdh) === false){
					_self.$attention.noIcon('请输入正确格式的手机号')
					return el['submit_loading'] = false
				}
				insertCbf(_self.params).then(res => {
					_self.$attention.noIcon(res.msg)
					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
						})
					}, 1000)
				}).finally(() => el['submit_loading'] = false)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.setting-container{
		font-weight: 500;
		min-height: 100vh;
		background: #f8f8f8;
		padding-bottom: 200rpx;
		.form-item{
			padding: 30rpx 24rpx 30rpx 54rpx;
			background: #fff;
			position: relative;
			&::before{
				visibility: hidden;
				content: "*";
				position: absolute;
				left: 24rpx;
				top: 55%;
				transform: translateY(-50%);
				color: #dd6161;
			}
			&.required::before{
				visibility: visible;
			}
			.form-label{
				font-size: 26rpx;
				color: #333;
			}
		}
	}
</style>
